﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Table_Dal;
namespace DAL
{
    public class ChuongDal:DataAccessLayer
    {
        DataDataContext dt;
        public List<ChuongTbl> DSChuong_Mon(string MaMon)
        {
            dt = GetKetnoi();
            List<ChuongTbl> dSChuong = new List<ChuongTbl>();
            IEnumerable<CHUONG> q = dt.CHUONGs.Where(k => k.MAMONHOC == MaMon);
            foreach (CHUONG item in q)
            {
                ChuongTbl Chuong = new ChuongTbl();
                Chuong.MaChuong = item.MACHUONG;
                Chuong.TenChuong = item.TENCHUONG;
                dSChuong.Add(Chuong);
            }
            return dSChuong;
        }
        
        public bool SuaChuong(ChuongTbl chg){
            dt = GetKetnoi();
            CHUONG chuong = dt.CHUONGs.SingleOrDefault(k => k.MACHUONG == chg.MaChuong);
            chuong.TENCHUONG = chg.TenChuong;
            dt.SubmitChanges();
            return true;
        }
        public bool ThemChuong(ChuongTbl chuong)
        {
            try
            {
                dt = GetKetnoi();
                CHUONG chg = new CHUONG();
                chg.MACHUONG = chuong.MaChuong;
                chg.TENCHUONG = chuong.TenChuong;
                chg.MAMONHOC = chuong.MaMonHoc;
                dt.CHUONGs.InsertOnSubmit(chg);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool XoaChuong(string MaChuong)
        {
            try
            {
                dt = GetKetnoi();
                CHUONG chg = dt.CHUONGs.SingleOrDefault(j => j.MACHUONG == MaChuong);
                dt.CHUONGs.DeleteOnSubmit(chg);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool KiemTra_TrungKhoa(string Ma)
        {
            dt = GetKetnoi();
            IEnumerable<CHUONG> ds = dt.CHUONGs.Where(j => j.MACHUONG == Ma);
            foreach (CHUONG  item in ds)
            {
                if (item.MACHUONG == Ma)
                    return true;
            }
            
            return false;
        }

        public List<string> LayTatCa_string()
        {
            dt = GetKetnoi();
            IEnumerable<CHUONG> ds = dt.CHUONGs;
            List<string> dslst = new List<string>();
            foreach (CHUONG item in ds)
            {
                dslst.Add(item.MACHUONG);  
            }

            return dslst;
        }
    }
}
